package com.top.service;

import com.top.dao.DAOFactory;
import com.top.dao.OrderDao;
import com.top.dao.OrderItemDao;
import com.top.em.MenuEnum;
import com.top.em.TipEnum;
import com.top.entiy.Food;
import com.top.entiy.Order;
import com.top.entiy.OrderItem;
import com.top.utils.SoutUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author:
 * @DateTime: 2023/12/10 09:09
 * @Description: TODO
 */
public class OrderService {

    /**
     * 查询当前服务中桌号订单数量
     *
     * @param integer
     */
    public int countByTableNoActive(Integer integer) {
        return DAOFactory.getOrderDAOInstance().countByTableNoActive(integer);
    }

    public boolean createOrder(ArrayList<OrderItem> foodList, Integer tableNo) {
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (OrderItem orderItem : foodList) {
            BigDecimal amount = orderItem.getAmount();
            totalAmount = totalAmount.add(amount);
        }
        Order order = new Order();
        // System.currentTimeMillis() 获取当前时间戳
        order.setSn(String.valueOf(System.currentTimeMillis()));
        order.setStatus(1);
        order.setTableNo(tableNo);
        order.setAmount(totalAmount);
        Integer key = DAOFactory.getOrderDAOInstance().createReturnKey(order);
        // 事务
        if (key == null) {
            System.out.println("订单创建失败");
            return false;
        }
        for (OrderItem orderItem : foodList) {
            orderItem.setOrderId(key);
            boolean b = DAOFactory.getOrderItemDAOInstance().doCreate(orderItem);
            if (!b) {
                System.out.println("订单创建失败");
                return false;
            }
        }
        return true;
    }

    public void index() {
        System.out.println("-------" + MenuEnum.CATEGORY_FOOD.getTip() + "--------");
        Scanner scanner = new Scanner(System.in);
        System.out.println(TipEnum.COMMON.getTip());
        SoutUtil.printButton(MenuEnum.DETAIL, MenuEnum.END_ORDER);
        List<Order> all = DAOFactory.getOrderDAOInstance().findAll();
        System.out.println("-------订单列表--------");
        System.out.println(new Order().getColumn());
        if (all != null && all.size() > 0) {
            for (Order order : all) {
                System.out.println(order.toString());
            }
        }
        String next = scanner.next();
        try {
            if (next.startsWith("D")) {
                detail();
            } else if (next.startsWith("E")) {
                orderEnd();
            } else if (next.startsWith("Q")) {
                new CommonService().index();
            }else {
                System.err.println("输入错误");
                index();
            }
        } catch (Exception e) {
            System.err.println("输入错误");
            index();
        }
    }

    private void orderEnd() {
        System.out.println("请输入订单ID");
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        boolean b = DAOFactory.getOrderDAOInstance().doUpdateStatus(id, 2);
        if (b) {
            System.out.println("订单已结束");
        } else {
            System.out.println("订单结束失败");
        }
        index();
    }

    private void detail()  {
        System.out.println("请输入订单ID");
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        Order order = DAOFactory.getOrderDAOInstance().findById(id);
        if (order == null) {
            System.out.println("订单不存在");
            index();
        }
        System.out.println("-------" + MenuEnum.DETAIL.getTip() + "--------");
        SoutUtil.printButton();
        List<OrderItem> list = DAOFactory.getOrderItemDAOInstance().findAllByOrderId(order.getId());
        System.out.println(new OrderItem().getColumn());
        for (OrderItem orderItem : list) {
            System.out.println(orderItem.toString());
        }
        String next = scanner.next();
        if (next.startsWith("Q")) {
            index();
        }else {
            System.err.println("输入错误");
            detail();
        }

    }
}
